﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOFrameworks;
using System.Collections;
using FooFun.DataElements.Dtos;
using System.Data.Common;

namespace PhysicalDataProvider
{
    public class ForumService
    {
        DEDataProvider dataProvider;

        public DEDataProvider DataProvider
        {
            get { return dataProvider; }
            set { dataProvider = value; }
        }

        string[] selectCols = new string[] { 
            "ForumId", "ForumName", "ParentId", "Theme","Note"};

        string[] updateCols = new string[] { 
            "Note"};

        string talbe = "t_Forums";

        string idCol = "ForumId";

        public IDataElement GetElement(IDataElement de)
        {
            string str = DataProviderUtil.GetGuidBase64(de.Value);

            IDataElement dbDE = DataProvider.GetScalar(str, selectCols, talbe, idCol);

            ForumDataElement objDE = new ForumDataElement(dbDE);

            return objDE;
        }

        public IDataElement SelectAllElement()
        {
            IDataElement dbDE
                = DataProvider.Select(selectCols, talbe, null);
            return dbDE;
        }

        public IDataElement ListForum()
        {
            IDataElement dbDE
                   = DataProvider.Select(selectCols, talbe, null);

            ForumListDataElement list = new ForumListDataElement(dbDE);

            return list;
        }

        class ForumDataElement : CustomDataElement, IEnumItem
        {
            public ForumDataElement(IDataElement dbDE)
                : base(dbDE)
            {

            }

            public override IDataElement this[string attrName]
            {
                get
                {
                    switch (attrName)
                    {
                        case "ForumId":
                            return DataProviderUtil.GetGuid
                                (base[attrName]);

                        default:
                            break;
                    }

                    return base[attrName];
                }
                set
                {
                    base[attrName] = value;
                }
            }

            #region IEnumItem 成员

            public string Name
            {
                get
                {
                    return Convert.ToString(this["ForumName"]);
                }
            }

            public Guid Id
            {
                get
                {
                    return DataProviderUtil.GetGuid(
                        this["ForumId"].Value);
                }
            }

            #endregion
        }

        class ForumListDataElement : CustomDataElement
        {
            public ForumListDataElement(IDataElement de)
                : base(de)
            {

            }

            public override IEnumerator GetEnumerator()
            {
                foreach (IDataElement item in this.InnerDataElement)
                {
                    yield return new ForumDataElement(item);
                }
            }
        }

        public void EditNote(Guid guid, string p)
        {
            DbParameter para1 = DataProvider.CreateParameter();
            para1.Value = DataProviderUtil.GetGuidBase64(guid);

            DbParameter para2 = DataProvider.CreateParameter();
            para2.Value = p;

            DataProvider.ExecuteNonQuery(
                "UPDATE t_Forums SET [Note]=? WHERE ForumId=?",
                para2, para1);
        }
    }
}
